package com.pocket.insight.base.context;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/11/29
 */
public class CancelContext {

    private static ConcurrentHashMap<String, List<Runnable>> requestCancelHandlerMap = new ConcurrentHashMap<>();

    /**
     * TODO
     * @param requestId
     * @param requestCancelHandler
     */
    public static void setRequestCancelHandler(String requestId, Runnable requestCancelHandler) {
        if (StringUtils.isNotEmpty(requestId)) {
            requestCancelHandlerMap.merge(requestId, new ArrayList<>(Arrays.asList(requestCancelHandler)), (x, y) -> {
                x.addAll(y);
                y.clear();
                return x;
            });
        }
    }

    /**
     * TODO
     * @param requestId
     */
    public static String getSessionRequestId(String requestId) {
        if (StringUtils.isNotBlank(requestId)) {
            return "SessionContext.getSessionId() + \"_\" + SessionContext.getSessionId()";
        }
        return null;
    }
}
